//
//  MYGImagesShowView.swift
//  MYGrow
//
//  Created by lance on 2024/3/2.
//

import UIKit
import QMUIKit

class MYGImagesShowView: UIView {
    // MARK: - Property
    var contentInset: UIEdgeInsets = UIEdgeInsets(top: 15, left: 15, bottom: 0, right: 15)
    var itemSpacing = 7.5
    var lineSpacing = 7.5
    var itemPerRow = 3
    var itemModels: [MYGImagesShowViewImageModel] = []
    
    
    // MARK: - init
    override init(frame: CGRect) {
        super.init(frame: frame)
        configUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - UI
    private func configUI() {
        addSubview(collectionView)
        collectionView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
            make.height.equalTo(0)
        }
    }
    
    private func itemWidth() -> CGFloat {
        let contentMargin = Double(30)
        let containerMargin = contentInset.left + contentInset.right
        let totalItemSpacing = itemSpacing > 1 ? itemSpacing * (Double(itemPerRow) - 1.0) : 0
        let total = contentMargin + containerMargin + totalItemSpacing
        let width = (screenWidth - total) / Double(itemPerRow)
        return width
    }
    
    // MARK: - Public funcs
    func reload(with imageModels: [MYGImagesShowViewImageModel]) {
        itemModels = imageModels
        let rows = ceil(Double(imageModels.count) / Double(itemPerRow))
        collectionView.snp.updateConstraints { make in
            make.height.equalTo(rows * itemWidth() + (rows == 0 ? 0 : (rows - 1) * lineSpacing) + contentInset.top + contentInset.bottom)
        }
        collectionView.reloadData()
    }
    
    // MARK: - lazy load
    private lazy var collectionView: UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
        collectionView.backgroundColor = .clear
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.register(cellWithClass: MYGImagesShowViewImageCell.self)
        return collectionView
    }()
}

extension MYGImagesShowView: UICollectionViewDelegate, UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        itemModels.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withClass: MYGImagesShowViewImageCell.self, for: indexPath)
        cell.model = itemModels[indexPath.row]
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        guard let cell = collectionView.cellForItem(at: indexPath) as? MYGImagesShowViewImageCell else { return }
        let vc = QMUIImagePreviewViewController()
        vc.imagePreviewView.delegate = self
        vc.imagePreviewView.currentImageIndex = UInt(indexPath.row)
        vc.sourceImageView = {
            return cell.imageView
        }
        vc.presentingStyle = .zoom
        vc.dismissingStyle = .fade
        UIViewController.currentViewController?.present(vc, animated: true)
    }
}

extension MYGImagesShowView: UICollectionViewDelegateFlowLayout {
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    
        CGSize(width: itemWidth(), height: itemWidth())
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        lineSpacing
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        itemSpacing
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        contentInset
    }
}

extension MYGImagesShowView: QMUIImagePreviewViewDelegate {
    func numberOfImages(in imagePreviewView: QMUIImagePreviewView!) -> UInt {
        return UInt(itemModels.count)
    }
    
    func imagePreviewView(_ imagePreviewView: QMUIImagePreviewView!, renderZoomImageView zoomImageView: QMUIZoomImageView!, at index: UInt) {
        guard let cell = collectionView.cellForItem(at: IndexPath(item: Int(index), section: 0)) as? MYGImagesShowViewImageCell else { return }
        zoomImageView.image = cell.imageView.image
    }
}
